<?php
/**
 *  搜索
 *  仲伟涛 2011-6-3
 */
class Dream_Page_Cms_Search extends Dream_Page_Abstract
{
    private $db;
    private $tblTitleName = 'cms_document_title_';

    public function validate(ME_Request $input, ME_Response $output)
    {
        $output->appId   = $input->appId  = 1;

        $output->page    =
        $input->page     =max((int)$input->get('page'),1);

        $output->keyword =
        $input->keyword  = trim($input->get("keyword"));
        
        return true;
    }

    private function loadDb()
    {
        $this->db = Db_Admin::instance();
    }

    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $this->loadDb();
        $keyword = str_replace(array('梦到','梦见','梦着','请输入关键字'), '', $input->keyword);

        //频道信息
        $output->classInfo =
        $classInfo = $this->loadCache("Cms_Class",array('appId'=>$input->appId));
        
        if($keyword){
            $subWhere .= " AND title like '%{$keyword}%' ";
        
            $subWhere = $subWhere ? ' WHERE status = 3  '.$subWhere : '';
            //获得所有记录
            $param = array(
                    'db'          => $this->db,
                    'fromSql'     => " from {$this->tblTitleName}{$input->appId} ", #from部分的语句
                    'orderBySql'  => ' order by id desc ',
                    'whereSql'    => $subWhere,
                    'pagesize'    => 80,
                    'page'        => $input->page,
                    'urlTpl'      => 'index.php?c='.$input->ctlName.'&page={PAGE}',
            );
            $listData = Libs_DBHelper::getList($param);
            $output->pageBar = $listData['pageBar'];
            $output->list = $listData['list'];
            if(count($output->list) == 1){#如果一个，就直接显示该页面
                $doc = $output->list[0];
                $url = Libs_Cms_Document::getDocumentUrl('',$classInfo[$doc['classId']]['path'], $doc['id'], $doc['enfile']);
                header("Location:{$url}");
                exit;
            }
        }
        $output->setTemplate(PRODUCTION_ROOT . "/App/Admin/Skin/Template/{$input->appId}/Cms/Search",0,true);
    }
    
    
}
